<?php

namespace database;

use PDO;

class DB
{
    protected $link;

    protected $options = [
        'table' => '', 'where' => '', 'order' => '',
        'limit' => '', 'field' => ' * '
    ];

    public function __construct($config)
    {
        $this->connect($config);
    }

    protected function connect($config)
    {
        $dsn = sprintf("mysql:host=%s;dbname=%s;charset=%s"
            , $config['host'], $config['database'], $config['charset']);
        $this->link = new PDO($dsn, $config['user'], $config['password'],
            [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]);
    }

    public function query($sql, $vars = [])
    {
        // 生成预处理语句
        $statement = $this->link->prepare($sql);
        $statement->execute($vars);
        // 返回结果
        return $statement->fetchAll();
    }

    public function execute($sql, $vars = [])
    {
        $statement = $this->link->prepare($sql);
        return $statement->execute($vars);
    }

    public function table($table)
    {
        $this->options['table'] = $table;
        return $this;
    }

    public function field(...$fields)
    {
        $this->options['field'] = '`'.implode('`,`', $fields).'`';
        return $this;
    }

    public function limit(...$limit)
    {
        $this->options['limit'] = ' LIMIT ' . implode(',', $limit);
        return $this;
    }

    public function order($order) {
        $this->options['order'] = ' ORDER BY ' . $order;
        return $this;
    }

    public function where($where) {
        $this->options['where'] = " WHERE " . $where;
        return $this;
    }

    public function get()
    {
        $sql = "SELECT {$this->options['field']} FROM {$this->options['table']} {$this->options['where']}
         {$this->options['order']} {$this->options['limit']}";
        return $this->query($sql);
    }

    public function insert(array $vars) {
        // INSERT INTO new () VALUES()
        $fields = '`'. implode("`,`", array_keys($vars)). '`';
        $values = implode(',', array_fill(0, count($vars), '?'));
        $sql = "INSERT INTO {$this->options['table']} ($fields) VALUES($values)";
        return $this->execute($sql, array_values($vars));
    }

    public function update(array $vars) {
        if (empty($this->options['where'])) {
            throw new \Exception('更新必须设置条件');
        }
        $sql = "UPDATE {$this->options['table']} SET ".implode("=?,", array_keys($vars))
                ." =? {$this->options['where']}";
        return $this->execute($sql, array_values($vars));
    }

    public function delete() {
        // DELETE FROM news WHERE ...
        if (empty($this->options['where'])) {
            throw new \Exception('删除必须设置条件');
        }
        $sql = "DELETE FROM {$this->options['table']} {$this->options['where']}";
        // echo $sql;
        return $this->execute($sql);
    }
}